package com.imooc.demo.web;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.imooc.demo.entity.Admin;
import com.imooc.demo.entity.Category1;
import com.imooc.demo.entity.Category2;
import com.imooc.demo.entity.ResultBean;
import com.imooc.demo.service.AdminService;
import com.imooc.demo.service.Category1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private Category1Service category1Service;

    /**
     * 跳转到用户登录页面
     * @return 登录页面
     */
    @RequestMapping("/login2")
    public Object loginHtml() {
        return "html/login";
    }
    @RequestMapping("/index")
    public Object indexHtml() {
        return "html/index";
    }

    @RequestMapping("/welcome")
    public Object welcomeHtml() {
        return "html/welcome";
    }


    @RequestMapping("/weixiu-category")
    public Object weixiuCategoryHtml() {
        return "html/weixiu-category";
    }
    @RequestMapping("/weixiu-list")
    public String  weixiuListHtml(Model model) {
        List<Category1> list = category1Service.getCategory1List();
        System.out.println(list.get(0));
        model.addAttribute("category1List",list);
        return "html/weixiu-list";
    }


    @RequestMapping(value = "/category1update")
    private String  modifyCategory1(Model model,
                                          @Valid Category1 category1)
            throws JsonParseException , JsonMappingException , IOException {

        category1Service.modifyCategory1(category1);
        List<Category1> list = category1Service.getCategory1List();
        System.out.println(list.get(0));
        model.addAttribute("category1List",list);
        return "html/weixiu-list";

    }
   /* @RequestMapping("/category-edit")
    public Object  categoryEditHtml() {
        return "html/category-edit";
    }*/

    /*@RequestMapping("/category-edit")
    public Object  categoryEditHtml( Model model,HttpServletRequest request) {
        Category1 category1 = category1Service.getCategory1ById(Integer.parseInt(request.getParameter("category1Id")));
        System.out.println(category1.toString());
        model.addAttribute("category1",category1);
        return "html/category-edit";
    }*/
    @RequestMapping("/category-edit")
    public Object  categoryEditHtml( HashMap<String, Object> map,HttpServletRequest request) {
        Category1 category1 = category1Service.getCategory1ById(Integer.parseInt(request.getParameter("category1Id")));
        System.out.println(category1.toString());
        map.put("category1",category1);
        return "html/category-edit";
    }

    // 一对多查询
    @RequestMapping(value = "/category2-list")
    private  Object  selectCategory1ById(HashMap<String, Object> map,HttpServletRequest request){
        Category1 category1 = category1Service.AselectCategory1ById(Integer.parseInt(request.getParameter("category1Id")));
        System.out.println(category1.toString());
        //Set<Category2> category2List= category1.getCategory2s();
        map.put("category1",category1);
        return "html/category2-list";
    }



    //修改种类信息
   /* @RequestMapping(value = "/category1update")
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等
    private String  modifyCategory1(@ModelAttribute @Valid Category1 category1)
            throws JsonParseException , JsonMappingException , IOException {
       System.out.println(category1.toString());


           category1Service.modifyCategory1(category1);
           return "html/success-prompt";


    }*/
    /*@RequestMapping(value = "/category1update")
    private ModelAndView  modifyCategory1(ModelAndView modelAndView,
                                          @Valid Category1 category1)
            throws JsonParseException , JsonMappingException , IOException {

        category1Service.modifyCategory1(category1);
        return new ModelAndView("forward:/weixiu-list");

    }*/




   /*@RequestMapping(value = "/category1update")
    private ModelAndView  modifyCategory1(ModelAndView modelAndView,
                                    @Valid Category1 category1, BindingResult bindingResult)
            throws JsonParseException , JsonMappingException , IOException {
        if(bindingResult.hasErrors()){
            modelAndView.addObject("error",bindingResult.getFieldError().getDefaultMessage());
            modelAndView.setViewName("html/failure-prompt");
            return modelAndView;
        }
        category1Service.modifyCategory1(category1);
        modelAndView.setViewName("html/success-prompt");
        return modelAndView;

    }*/

   //添加种类信息跳转页面
    @RequestMapping("/category1-add")
    public ModelAndView  categoryAddHtml(Model model ) {
        model.addAttribute("category1",new Category1());
        model.addAttribute("title","添加类别");
        return new ModelAndView("html/category1-add","category1Model",model);
    }
    //添加种类信息
    @RequestMapping(value = "/category1add")
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等,此处需传入xml
    private  ModelAndView  addCategory1(Category1 category1 ){

        category1Service.addCategory1(category1);
        return new ModelAndView("forward:/weixiu-list");
    }






    //删除种类信息
    @RequestMapping(value = "/category1delete")
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等,此处需传入xml
    private boolean  removeCategory1(Integer category1Id){
        return  category1Service.deleteCategory1(category1Id);
    }



    @RequestMapping("/login")
    @ResponseBody
    public Object login(HttpServletRequest request,
                        @RequestParam(value = "admin_id", required = false) Integer adminId,
                        @RequestParam(value = "admin_password", required = false) String adminPassword) {
        Map<String,Object> map = new HashMap<>();
        map.put("adminId",adminId);
        map.put("adminPassword",adminPassword);
        Admin admin = adminService.selectByAdminId(map);
        if(admin == null){
            return new ResultBean(-1,"用户名密码错误",null);
        }else{
            HttpSession session = request.getSession();
            session.setAttribute("admin",admin);
        }
        return new ResultBean(200,"登陆成功",admin);
    }












    }
